package com.apes.fa.model;

import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.frame.model.DutyCenter;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.repository.PartyRoleRepository;
import com.apes.scm.masterdata.product.model.Product;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = FaAssetsTransferOutItem.CONST_TABLE_NAME)
public class FaAssetsTransferOutItem extends Model {

    public static final String CONST_TABLE_NAME = "fa_assets_transfer_out_item";

    @Id
    @Column(name = "id", nullable = false, length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "资产转出明细(账套内)")
    @Comment("资产转出明细(账套内)编码")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fa_card_id", referencedColumnName = "id")
    @Comment("资产建卡")
    private FaCard faCard;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fa_assets_transfer_out_id", referencedColumnName = "id", nullable = false)
    @Comment("资产转出(账套内)")
    private FaAssetsTransferOut assetsTransferOut;

    @Column(name = "old_dept_id")
    @Comment("原部门编码")//对应发货部门
    private String oldDeptId;

    @Column(name = "old_dept_name")
    @Comment("原部门名称")
    private String oldDeptName;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id")
    @Comment("部门")
    private Dept dept;

    @Column(name = "old_responsible_person")
    @Comment("原负责人")
    private String oldResponsiblePerson;

    @Column(name = "old_custodian")
    @Comment("原保管人")
    private String oldCustodian;

    @Comment("负责人")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "principal_id", referencedColumnName = "id")
    private PartyRole principal;

    @Comment("保管人")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "custodian_id", referencedColumnName = "id")
    private PartyRole custodian;

    @Comment("发货人")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "consigner_id", referencedColumnName = "id")
    private PartyRole consigner;

    @Comment("验收人")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "acceptancer_id", referencedColumnName = "id")
    private PartyRole acceptancer;

    @Column(name = "send_mark")
    @Comment("已发货标识")
    private boolean sendMark;

    @Column(name = "receipt_mark")
    @Comment("已收货标识")
    private boolean receiptMark;

    @Column(name = "send_date")
    @Comment("发货日期")
    private Date sendDate;

    @Column(name = "acceptance_date")
    @Comment("验收日期")
    private Date acceptanceDate;

    @States(name = "发货收货勾选标记", values = {
            @State(id = "false", name = "不选择"),
            @State(id = "true", name = "选择")
    })
    @Column(name = "inquiry_select_state")
    private boolean inquirySelectState;

    @Column(name = "note")
    @Comment("摘要")
    private String note;

    @Transient
    private Product product;
    @Transient
    public Product getProduct() {
        if (faCard!=null) return faCard.getProduct();
        return null;
    }

    @Transient
    private PartyRole _creator;

    @Transient
    public PartyRole get_creator() {
        if(this.getCreator() == null) return null;
        return SpringManager.getBean(PartyRoleRepository.class).findOne(this.getCreator());
    }


    @Transient
    private String sendMarkView;

    public String getSendMarkView() {
        return sendMark ? "是" : "否";
    }

    @Transient
    private String receiptMarkView;

    public String getReceiptMarkView() {
        return receiptMark ? "是" : "否";
    }


}
